<style>
.snapshot .edit_actions a {
  visibility: hidden;
}

.snapshot:hover .edit_actions a {
  visibility: visible;
}
.snapshot .create_actions a {
  display: none;
}

.snapshot:hover .create_actions a {
  display: inline;
}
</style>

<h1><%= message('page.redmine_configuration.name') -%></h1>

<br />

<% javascript_tag do -%>
  var trackerId = <%= @tracker_id %>;
  var priorityId = <%= @priority_id %>;

  function clearErrors() {
    $j('table div[id$="_error"]').hide();
  }

  function startTest() {
    clearErrors();

    if (!$j('#testStatus').is(':visible')) {
      $j('#testStatus').show();
    } else {
      $j('#testStatus').html('<%= image_tag 'loading.gif' -%>');
    }

    $j('#api_key_user_div').hide();
  }

  function finishedSave(request) {
    var r = jQuery.parseJSON (request.responseText);

    if (r.status == 'error') {
      handleErrors(r.error);
    } else if (r.status == 'success') {
      $j('#infomsg').html('<%= message('page.redmine_configuration.settings.saved') -%>');
      $j('#info').show();
      Element.hide.delay(5, 'info');
    }
  }

  function handleErrors(errors) {
    jQuery.each(errors, function(key, value) {
      var t = $j('#' + key +'_error');

      if (t != undefined) {
        t.html(value);
        t.show();
      } else if (key == 'general') {
        $j('#errormsg').html(value);
        $j('#error').show();
      }
    });
  }

  function finishedTest(response) {
    if (response.status == 'error') {
      $j('#testStatus').html('<%= image_tag 'exclamation.png' -%>');

      handleErrors(response.error);
    } else if (response.status == 'success') {
      $j('#testStatus').html('<%= image_tag 'accept.png' -%>');

      updateInfo('#api_key_user', response.user);
      updateInfo('#project_key_project', response.project);

      updateSelect('#priority_id', response.priorities);
      updateSelect('#tracker_id', response.trackers);
	  
      selectValueInSelectList('#tracker_id', trackerId);
      selectValueInSelectList('#priority_id', priorityId);

      checkSelection();
    }
  }

  function updateInfo(target, value) {
    var target = $j(target);

    if (target != undefined) {
      target.html(value);
      target.parent().show();
    }
  }

  function updateSelect(target, list) {
    clearList(target);
    var target = $j(target);
    jQuery.each(list, function(key, value) {
        target.append('<option value=' + key + '>' + value + '</option>');
    });

    target.prop('disabled', false);
  }

  function clearList(target) {
    var target = $j(target);

    target.empty()
    target.append('<option value=0>-</option>');
  }

  function checkSelection() {
    if ($j('#priority_id').value != 0 && $j('#tracker_id').value != 0) {
      $j('#submit_save').prop('disabled', false);
    } else {
      $j('#submit_save').prop('disabled', true);
    }
  }

  function selectValueInSelectList(target, value) {
    var targetElement = $j(target);
    if (targetElement == undefined) {
      return;
    }

    var found = false;

    $j(function() {
      $j(target + ' option').each(function(i){
        if ($j(this).val() == value) {
          found = true || found;
        }
      });
    });

    if (found) {
      targetElement.val(value);
    } else {
      targetElement.val(0);
    }
  }

  <% if @run_test %>
  $j(function() {
    $j('#loading').show();

    startTest();
<!--$j('#redmineConfigurationForm').serialize()-->

    $j.ajax({url:'<%= url_for(:action => 'test') %>',
      data:$j('#redmineConfigurationForm').serialize(),
      type: "POST",
      success:function(request){
          $j('#loading').hide();
          finishedTest(request);
      },
      error: function (xhr, ajaxOptions, thrownError) {
        $j('#loading').hide();
        alert(xhr.status);
        alert(thrownError);
      }
    });
    
  });
  <% end %>
<% end -%>

<div id="redmineConfigurationFields" style="position:relative;display:block">

<div id="loading" style="display:none; position: absolute;
  width: 100%; height: 100%;
    text-align:center; background:#fff;
  padding-top: 50px;
    filter: alpha(opacity=75); /* internet explorer */
    -khtml-opacity: 0.75;      /* khtml, old safari */
    -moz-opacity: 0.75;       /* mozilla, netscape */
    opacity: 0.75;           /* fx, safari, opera */
    border-top:1px solid #ddd;
    border-bottom:1px solid #ddd;">
  <p><h2><%= image_tag 'loading.gif' -%> <%= message('page.redmine_configuration.settings.loading') -%></h2></p>
</div>

<% form_remote_tag :url => {:action => 'save'},
           :html => {:id => 'redmineConfigurationForm'},
                     :method => :post,
                     :before => "$j('#submit_save').prop('disabled', true);",
           :complete => "finishedSave(request); $j('#submit_save').prop('disabled', false);",
                     :script => false do -%>
  <input type="hidden" name="resource" value="<%= params[:resource] -%>"/>

  <table class="data form marginbottom10">
    <thead>
    <tr>
      <th colspan="3"><span><%= message('page.redmine_configuration.settings.general') -%></span></th>
    </tr>
    </thead>
    <tbody>
    <tr class="even">
      <td colspan="3"><span><%= message('page.redmine_configuration.settings.redmine_access') -%></span></td>
    </tr>
    <tr class="odd">
      <td class="keyCell"><label for="purl"><%= message('page.redmine_configuration.settings.url') -%>:</label></td>
      <td>
        <div><%= text_field_tag 'purl', @purl %></div>
    <div class="note"><%= message('page.redmine_configuration.settings.url.global') -%>: <%= @gurl.empty? ? message('page.redmine_configuration.settings.url.global.not_set') : @gurl %></div>
    <div class="error" id="url_error" style="display:none;" />
    </td>
      <td class="comments"><%= message('page.redmine_configuration.settings.url.description') -%></td>
    </tr>
    <tr class="even">
      <td class="keyCell"><label for="api_key"><%= message('page.redmine_configuration.settings.api_key') -%>:</label></td>
      <td>
        <%= text_field_tag 'api_key', @api_key %>
    <div class="note"><% if !@gurl.blank? and !@gapi_key.blank? %><%= message('page.redmine_configuration.settings.api_key.global.is_set') -%><% else %><%= message('page.redmine_configuration.settings.api_key.global.not_set') -%><% end %></div>
    <div class="note" id="api_key_user_div" style="display:none;"><%= message('page.redmine_configuration.settings.api_key.user') -%>: <span id="api_key_user"></span></div>
    <div class="error" id="api_key_error" style="display:none;" />
    </td>
      <td class="comments"><%= message('page.redmine_configuration.settings.api_key.description') -%></td>
    </tr>
    <tr class="odd">
      <td colspan="3"><span><%= message('page.redmine_configuration.settings.project') -%></span></td>
    </tr>
    <tr class="even">
      <td class="keyCell"><label for="project_key"><%= message('page.redmine_configuration.settings.project_key') -%>:</label></td>
      <td>
        <%= text_field_tag 'project_key', @project_key %>
    <div class="note" id="project_key_project_div" style="display:none;"><%= message('page.redmine_configuration.settings.project_key.project') -%>: <span id="project_key_project"></span></div>
    <div class="error" id="project_key_error" style="display:none;" />
    </td>
      <td class="comments"><%= message('page.redmine_configuration.settings.project_key.description') -%></td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
      <td colspan="3">
      <div>
        <%= submit_to_remote "submit_test",
                             message('page.redmine_configuration.settings.test'),
                             :url => {:action => 'test'},
                             :html => {:id => "submit_test"},
                             :before => "$j('#submit_test').prop('disabled', true); startTest();",
                             :complete => "finishedTest(jQuery.parseJSON(request.responseText)); $j('#submit_test').prop('disabled', false);" -%>
        <span id="testStatus" style="display: none;"><%= image_tag 'loading.gif' -%></span>
      </div>
      </td>
    </tr>
    </tfoot>
  </table>

<br/>

  <table class="data form marginbottom10">
    <thead>
    <tr>
      <th colspan="3"><span><%= message('page.redmine_configuration.settings.issue') -%></span></th>
    </tr>
    </thead>
    <tfoot>
    <tr>
      <td colspan="3">
        <%= submit_tag message('page.redmine_configuration.settings.save'), :id => 'submit_save', :disabled => true %>
      </td>
    </tr>
    </tfoot>
    <tbody>
    <tr class="even">
      <td class="keyCell"><label for="to_address"><%= message('page.redmine_configuration.settings.priority_id') -%>:</label></td>
      <td><%= select_tag "priority_id", "<option value='0'>-</option>", :disabled => true, :onchange => "checkSelection()" %></td>
      <td class="comments"><%= message('page.redmine_configuration.settings.priority_id.description') -%></td>
    </tr>
    <tr class="odd">
      <td class="keyCell"><label for="subject"><%= message('page.redmine_configuration.settings.tracker_id') -%>:</label></td>
      <td><%= select_tag "tracker_id", "<option value='0'>-</option>", :disabled => true, :onchange => "checkSelection()" %></td>
      <td class="comments"><%= message('page.redmine_configuration.settings.tracker_id.description') -%></td>
    </tr>
    </tbody>
  </table>
<% end -%>
</div>
